package com.cssw.fyzb.core.dao.msg;

import com.cssw.fyzb.core.model.dodb.SysMessage;
import com.cssw.fyzb.core.model.dodb.SysSystemMsg;
import com.cssw.fyzb.core.model.dodb.SystemMsg;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Repository
public class MsgDaoImpl implements MsgDao {

    @Override
    public Page<SysMessage> list(int page, int limit, String state) {
        Map<String, Object> str = new HashMap<>();
        str.put("state", state);
        SqlParse sql = new SqlParse(str);
        sql.addSQL("select * from sys_message where state=:state order by msg_id desc  ");
        return SysMessage.dao.paginate(page, limit, sql);
    }

    public Page<SysMessage> listMsg(int page, int limit, Map param) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select * from sys_message where state=1 ");
        sqlParse.addLikeSQL("  and title like :title   ");
        sqlParse.addLikeSQL("  and content like :content ");
        sqlParse.addSQL(" order by  msg_id desc ");
        return SysMessage.dao.paginate(page, limit, sqlParse);
    }


    public void save(String title, String content, Long operId) {
        SysMessage systemMsg = new SysMessage();
        systemMsg.setCreateTime(new Date());
        systemMsg.setContent(content);
        systemMsg.setTitle(title);
        systemMsg.setCreateOperId(operId);
        systemMsg.save();
    }


    public void update(Long msgId, String title, String content, Long operId) {
        SysMessage systemMsg = new SysMessage();
        systemMsg.setUpdateTime(new Date());
        systemMsg.setContent(content);
        systemMsg.setTitle(title);
        systemMsg.setMsgId(msgId);
        systemMsg.setUpdateOperId(operId);
        systemMsg.update();
    }

    public void updateStatus(Long msgId, String status, Long operId) {
        SysMessage sysMessage = SysMessage.dao.findById(msgId);
        sysMessage.setState(status);
        sysMessage.setUpdateOperId(operId);
        sysMessage.setUpdateTime(new Date());
        sysMessage.update();
    }

    @Override
    public List<SysMessage> listTitle(String title) {
        StringBuilder str = new StringBuilder();
        str.append(" select * from sys_message where title=? ");
        return SysMessage.dao.find(str.toString(), title);
    }

    @Override
    public List<SysMessage>  getTitle(String title, Long msgId) {
        StringBuilder str = new StringBuilder();
        str.append(" select * from sys_message where title=? and  msg_id=? ");
        return SysMessage.dao.find(str.toString(), title, msgId);
    }

    @Override
    public SysMessage detail(Long msgId){
        return SysMessage.dao.findById(msgId);
    }
}
